package com.tencent.sr.iris.basic.mp.dependency.activity;

import com.alibaba.fastjson.JSON;
import com.tencent.sr.iris.activity.interfaces.client.activity.UserEventReportClient;
import com.tencent.sr.iris.activity.interfaces.request.acticity.UserOpenAppRequest;
import com.tencent.sr.rmall.common.exception.retail.TRetailBizException;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class UserEventReportDependency {

    @Resource
    private UserEventReportClient userEventReportClient;
    
    public HttpResult<Boolean> userOpenApp(UserOpenAppRequest req){

        HttpResult<Boolean> result = userEventReportClient.userOpenApp(req);
        if(result == null){
            log.error("userOpenApp error result is null uid:{}", req.getUid());
            throw new TRetailBizException("上报用户打开小程序事件失败，返回结果为null");
        }
        if(!result.isSuccess() || !Boolean.TRUE.equals(result.getData())){
            log.error("userOpenApp error result is null uid:{},result:{}", req.getUid(), JSON.toJSON(result));
            throw new TRetailBizException("上报用户打开小程序事件失败,errorMsg:" + result.getErrorMsg());
        }
        log.info("userOpenApp 上报打开小程序事件成功 uid:{}", req.getUid());
        return result;
    }
}

